package bo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import antlr.StringUtils;
import dao.hibernate.CauseVoDAO;
import dao.hibernate.StackTraceVoDAO;
import dao.hibernate.StuckEventVoDAO;
import dto.StackTraceDto;
import vo.CauseVo;
import vo.StackTraceVo;
import vo.StuckEventVo;

public class StacTraceBo {
	private static Logger LOGGER = LoggerFactory.getLogger(StacTraceBo.class);
	private CauseVoDAO dao =new CauseVoDAO();
	public List<CauseVo> findAll() {
		LOGGER.debug("finding all CauseVo instances");
		final List<CauseVo> result = new ArrayList<CauseVo>();
		final CauseVo e = new CauseVo();
		e.setUid(0);
		e.setKeyCause("全部");
		result.add(e);
		result.addAll(dao.findAll());
		return result;
	}
	public Map<String,String> getCauseUidString(){
		Map<String,String> result  = new HashMap<String,String>();
		List<CauseVo> list = dao.findAll();
		for(CauseVo vo : list ){
			result.put(vo.getUid().toString(), vo.getKeyCause());
		}
		return result;
	}
	public List<CauseVo> listAllForModify() {
		LOGGER.debug("finding all CauseVo instances");
		final List<CauseVo> result = new ArrayList<CauseVo>();
		final CauseVo e = new CauseVo();
		e.setUid(0);
		e.setKeyCause("其他");		
		result.addAll(dao.findAll());
		result.add(e);
		return result;
	}
	public StackTraceDto getStackTracePattern(final Integer keyCuaseUid){
		final StackTraceVoDAO stackTraceVoDAO =new  StackTraceVoDAO ();
		final	CauseVo causeVo = dao.findById(keyCuaseUid);		
		final	List<StackTraceVo> list = stackTraceVoDAO.findByKeyCause(keyCuaseUid.toString());
		return new StackTraceDto(causeVo , list);
	}
	public void insertNewCauseByStackTraceUid(final String desc,final Integer stackTraceUid){
		if (desc != null && desc.trim().length() != 0) {
			final CauseVoDAO causeVoDAO = new CauseVoDAO();
			final List<CauseVo> allsize = causeVoDAO.findAll();
			CauseVo transientInstance = new CauseVo();
			Integer newKeyCauseUid = allsize.size() + 1;
			transientInstance.setUid(allsize.size() + 1);
			transientInstance.setKeyCause(desc);
			causeVoDAO.save(transientInstance);
			final StackTraceVoDAO stackTraceVoDAO = new StackTraceVoDAO();			
			stackTraceVoDAO.updateKeyCauseUidByUid(stackTraceUid.toString(), newKeyCauseUid.toString());
		}
	}
	public void updateNewCauseByStackTraceUid(final Integer newKeyCauseUid,final Integer stackTraceUid){
		if (newKeyCauseUid != null ) { 
			final StackTraceVoDAO stackTraceVoDAO = new StackTraceVoDAO();			
			stackTraceVoDAO.updateKeyCauseUidByUid(stackTraceUid.toString(), newKeyCauseUid.toString());
		}
	}
//	public Map<String,Integer> getStaticsData(final Integer keyCuaseUid){
//		final Map<String,Integer>  result = new HashMap<String, Integer>();
//		final StuckEventVoDAO stuckEventVoDAO = new StuckEventVoDAO();
//		final StuckEventVoDAO dao =new StuckEventVoDAO();
//		List<String> allIps = dao.findDistinctHostIp();		
//		if(keyCuaseUid!=null && keyCuaseUid.intValue()==0){
//			//抓全部				
//			for(String hostIp : allIps){
//				final int	count = stuckEventVoDAO.countByCauseUid(hostIp);
//				result.put(hostIp, count);
//			}
//		}else if(keyCuaseUid!=null ){
//			for(String hostIp :allIps){
//				int count = stuckEventVoDAO.countByCauseUid(hostIp,keyCuaseUid);
//				result.put(hostIp, count);
//			}
//		}
//		return result;
//	}
	public Map<String,Integer> getStaticsData(final Integer keyCuaseUid ,final String startDate ,final String endDate){
		final Map<String,Integer>  result = new HashMap<String, Integer>();
		final StuckEventVoDAO stuckEventVoDAO = new StuckEventVoDAO();
		final StuckEventVoDAO dao =new StuckEventVoDAO();
		List<String> allIps = dao.findDistinctHostIp();		
		if(keyCuaseUid!=null && keyCuaseUid.intValue()==0){
			//抓全部				
			for(String hostIp : allIps){
				final int	count = stuckEventVoDAO.countByCauseUid(hostIp,startDate,endDate);
				result.put(hostIp, count);
			}
		}else if(keyCuaseUid!=null ){
			for(String hostIp :allIps){
				int count = stuckEventVoDAO.countByCauseUid(hostIp,keyCuaseUid,startDate,endDate);
				result.put(hostIp, count);
			}
		}
		
		return result;
	}
}
